package com.lagou.work02.util;


import com.lagou.work02.stereotype.MyAutowired;
import com.lagou.work02.stereotype.MyService;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@MyService
public class ConnectionUtils {

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); // 存储当前线程的连接

    public Connection getCurrentThreadConn() throws SQLException {
        Connection connection = threadLocal.get();
        if (connection == null) {
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }

}
